Home:ALL Converter>Is this Insertion Sort implementation worst case O(n)?

Is this Insertion Sort implementation worst case O(n)?

Ask Time:2018-11-13T01:21:05         Author:cyber_punkVCR

Json Formatter

I know that Insertion Sort is supposed to be worst case O(n^2), but I'm wondering why the following implementation isn't O(n).

void main()
{
//insertion sort runs from i = 1 to i = n, thus is worst case O(n)
for (

    int i = 1,
    placeholder = 0,
    A[] = { 10,9,8,7,6,5,4,3,2,1 },
    j = i;

    i <= 10;

    j-- > 0 && A[j - 1] > A[j]
    ? placeholder = A[j], A[j] = A[j - 1], A[j - 1] = placeholder
    : j = ++i
)

{
for (
    int x = 0;
    x < 10; x++
) 
    cout << A[x] << ' ';
    cout << endl;
}


system("pause");
}

There is only one for loop involved here and it runs from 1 to n. It seems to me that this would be the definition of O(n). What exactly am I missing here?

Author:cyber_punkVCR,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53267108/is-this-insertion-sort-implementation-worst-case-on
JaMiT :

Sloppy terminology has led many people to false conclusions. This appears to be an example.\n\n\n There is only one for loop involved here and it runs from 1 to n.\n\n\nYes, there is only one loop, but what is this \"it\" to which you refer? I really do mean for you to think about it. Should \"it\" refer to the loop? That would match a fairly common, yet sloppy, use of terminology, but a loop does not evaluate to a value. So a loop cannot actually run from one value to another. The sloppiness can be overlooked in simpler contexts, but not in yours.\n\nNormally, the \"it\" would really refer to the loop control variable. With a simple loop, like for (int i = 0; i < 10; ++i), there is a one-to-one correspondence between iterations of the loop and values assigned to the control variable (which is i in my example). So there is an equivalence present, allowing one to refer to the loop when one really means the control variable. Saying that a loop runs from x to y really means that the control variable runs from x to y, and that there is one iteration of the loop per value assigned to the control variable. This correspondence fails in your code.\n\nIn your loop, the thing that runs from 1 to n is i. However, i is not incremented with each iteration of the loop, so \"it runs from 1 to n\" is not an accurate assessment of your loop. When i is 1, there are up to 2 iterations. That's not a one-to-one correspondence between iterations and values of i. As i increases, the divergence from one-to-one grows. Each value of i potentially corresponds to i+1 iterations, as j counts down from i to 0. The total number of iterations in the worst case scenario for n entries is the sum of the potential number of iterations for each value of i: 2 + 3 + ⋯ + (n+1) = (n² + 3n)/2. That's O(n²).\n\nMoral of the story: writing compact, cryptic code does not magically change the complexity of the algorithm being implemented. Cryptic code can make the complexity harder to pin down, but the main thing you've accomplished is making your code harder to read.",
2019-08-23T05:43:29
kerastf :

Thats a very odd way to write code.But You have 2 for loops in the definition. It is not always necessary to have nested loops to have O(n^2), you can have it with recursion also. \nIn simple terms O(n^2)n simply means number of operations performed when the input size is n.",
2018-11-12T17:54:18
yy